<?php

require_once('../include/GeneralTop.php');
require('../include/class/db.php');

if (!$pay_member) {
	die('<div style="text-align:center">Permission denied...</div>');
}

if ((isset($_POST["submit"]))) {
    $db = new db;
    
    //handle pics
    /*********************BEGIN*************************/
    $sub_folder = date("Y-m-d");	
	$savePath = IMAGES_HAPPYHOME_SAVE_FOLDER.'/'.$sub_folder;
	if(!is_dir($savePath)) {
		mkdir($savePath, 0755);
	}
    
    $fileTemp1 = getimagesize($_FILES['pic1']['tmp_name']);
	$trueType1 = $fileTemp1['mime'];
	$fileTemp2 = getimagesize($_FILES['pic2']['tmp_name']);
	$trueType2 = $fileTemp2['mime'];
	$fileTemp3 = getimagesize($_FILES['pic3']['tmp_name']);
	$trueType3 = $fileTemp3['mime'];
	$fileSizeAll1 = $_FILES['pic1']['size'];
	$fileSizeAll2 = $_FILES['pic2']['size'];
	$fileSizeAll3 = $_FILES['pic3']['size'];
	if('' != $_FILES['pic1']['name']) {
		if(!in_array($trueType1, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll1 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//first pic
		$fileId = md5($_FILES["pic1"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic1']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name1 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic1']['tmp_name'], $imageFullName);
		
		//generate thumb of first pic
		$imageName_t = $fileId.'_t';
		$imageFullName_t = $savePath.'/'.$imageName_t.'.'.$extension;
		$file_name1_t = '/'.$sub_folder.'/'.$imageName_t.'.'.$extension;
		generateImages($trueType1, $imageFullName, PIC_THUMB_WIDTH, PIC_THUMB_HEIGHT, $imageFullName_t);
	}
	if('' != $_FILES['pic2']['name']) {
		if(!in_array($trueType2, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll2 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//second pic
		$fileId = md5($_FILES["pic2"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic2']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name2 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic2']['tmp_name'], $imageFullName);
	}
	if('' != $_FILES['pic3']['name']) {
		if(!in_array($trueType3, $typeArray)) {
			echo '<div align="center">The format of the pics you uploaded is not png,jpg,jpeg,gif, please upload again</div>';
			die();
		}
		if($fileSizeAll3 > $pic_size_limit * 1024) {
			echo '<div align="center">The size of the pics you uploaded exceed limitation, please upload another one</div>';
			die();
		}
		//third pic
		$fileId = md5($_FILES["pic3"]["tmp_name"] + rand()*100000);
		$imagePathInfo = pathinfo($_FILES['pic3']['name']);
		$extension = $imagePathInfo['extension'];
		$imageName = $fileId;
		$imageFullName = $savePath.'/'.$imageName.'.'.$extension;
		$file_name3 = '/'.$sub_folder.'/'.$imageName.'.'.$extension;
		move_uploaded_file($_FILES['pic3']['tmp_name'], $imageFullName);
	}	

	/*********************END**************************/
    
    $insertSQL = sprintf("INSERT INTO happyhome (haphome_class, haphome_title, haphome_info, haphome_author,
												haphome_pic, haphome_pic_thumb, haphome_pic2, haphome_pic3, add_user) 
												VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['haphome_class'], "int"),
					   GetSQLValueString($_POST['haphome_title'], "text"),
                       GetSQLValueString($_POST['haphome_info'], "text"),
                       GetSQLValueString($_POST['haphome_author'], "text"),
                       GetSQLValueString($file_name1, "text"),
                       GetSQLValueString($file_name1_t, "text"),
                       GetSQLValueString($file_name2, "text"),
                       GetSQLValueString($file_name3, "text"),
                       GetSQLValueString($_SESSION['MM_Username_id'], "int")
                       );

    $db->query($insertSQL);
    //header("location:".$_SERVER['PHP_SELF']."?result=suc");
    echo '<script>window.location.href="'.$_SERVER['PHP_SELF'].'?result=suc";</script>';
    exit();
}

include('AdminPanel.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" name="form1" id="form1">
  <table align="center">
    <?php
    if($_GET['result'] && 'suc' == $_GET['result']) {
    ?>
    <tr valign="baseline">
      <td colspan="2" nowrap="nowrap" align="center"><font color="green">添加成功...</font></td>
    </tr>
    <?php
    }
    ?>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">
      </select>
      标题:</td>
      <td><input type="text" name="haphome_title" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">详细信息:</td>
      <td><textarea name="haphome_info" cols="50" rows="5"></textarea></td>
    </tr>
    <tr>
      <td scope="row" align="right">分类:</td>
      <td><select name="haphome_class" id="haphome_class">
        <option value="2">快乐猫猫</option>
        <option value="1">快乐狗狗</option>
       
      </select></td>
   </tr>
   <tr valign="baseline">
      <td nowrap="nowrap" align="right">作者:</td>
      <td><input type="text" name="haphome_author" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">照片:</td>
      <td><input type="file" name="pic1" /> (列表页图，会自动生成缩略图)<br><input type="file" name="pic2" /><br><input type="file" name="pic3" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td>
      	<input type="submit" name="submit" id="submit" value="Insert record" />&nbsp;&nbsp;
      	<input type="button" id="button" onclick="window.location.href='ShowNews.php'" value="Back">
      </td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</body>
</html>